﻿using System;
using Cos.Core.NHibernate;
using Cos.Core.NHibernate.Tests;
using FluentNHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;

using global::NHibernate;

using NUnit.Framework;
using System.Collections.Generic;

namespace Cos.Module.Membership.NHibernate.Tests
{
    public class ModuleFixtureBase : FixtureBase
    {
        public ModuleFixtureBase() : base(new TestModel())
        {
            this.Culture = new CultureDataModel
            {
                Id = "fr-CA",
                CultureDesc = string.Empty,
                IsDefault = true
            };

            this.CustomUserFieldPk = new CustomUserFieldPK
            {
                Name = "fieldName",
                ApplicationName = ApplicationName
            };

            this.CustomUserField = new CustomUserFieldDataModel
            {
                //CustomUserField_Cultures = new List<CustomUserFieldCultureDataModel>(),
                //CustomUserField_UserValues = new List<CustomUserFieldUserValueDataModel>(),
                DataType = "string",
                IsRequired = false,
                Ordering = 1,
                //PK = this.CustomUserFieldPk
            };

            this.User = new UserDataModel
            {
                ApplicationName = ApplicationName,
                Comment = "comment",
                CreationDate = new DateTime(2011, 01, 06),
                Email = "email",
                FailedPasswordAnswerAttemptCount = 0,
                FailedPasswordAnswerAttemptWindowStart = null,
                FailedPasswordAttemptCount = 0,
                FailedPasswordAttemptWindowStart = null,
                IsApproved = true,
                IsLockedOut = false,
                IsOnline = false,
                LastActivityDate = new DateTime(2011, 01, 06),
                LastLockedOutDate = new DateTime(2011, 01, 06),
                LastLoginDate = new DateTime(2011, 01, 06),
                LastPasswordChangeDate = new DateTime(2011, 01, 06),
                Password = "password",
                PasswordAnswer = "password answer",
                PasswordQuestion = "password question",
                PKID = Guid.NewGuid(),
                Username = "username" + Guid.NewGuid()
            };

            this.Role = new RoleDataModel
            {
                ApplicationName = ApplicationName,
                //PageGroupAccesses = new List<PageGroupAccessDataModel>(),
                Rolename = "rolename",
                //UsersInRoles = new List<UsersInRoleDataModel>()
            };
        }

        public const string ApplicationName = "applicationName";

        /// <summary>
        /// Gets or sets culture
        /// </summary>
        public CultureDataModel Culture { get; set; }

        /// <summary>
        /// Gets or sets CustomUserField.
        /// </summary>
        public CustomUserFieldDataModel CustomUserField { get; set; }

        /// <summary>
        /// Gets or sets CustomUserFieldPk.
        /// </summary>
        public CustomUserFieldPK CustomUserFieldPk { get; set; }

        /// <summary>
        /// Gets or sets User
        /// </summary>
        public UserDataModel User { get; set; }

        /// <summary>
        /// Gets or sets Role
        /// </summary>
        public RoleDataModel Role { get; set; }

        [TestFixtureSetUp]
        public void ModuleSetupContext()
        {
            using (var tx = this.Session.BeginTransaction())
            {
                this.Session.SaveOrUpdate(this.Culture);
                this.Session.SaveOrUpdate(this.CustomUserField);
                this.Session.SaveOrUpdate(this.User);
                this.Session.SaveOrUpdate(this.Role);

                tx.Commit();
            }

            this.Session.Flush();
            this.Session.Clear();
        }
    }
}
